package com.since.data_structure.array;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Sincerity on 2020/5/4.
 * 描述：稀疏数组 将数组保存到本地 读取数组 转为二维数组
 */
public class SparseArray {

    public static void main(String[] args) throws IOException {
        //二维数组
        int[][] chessArrays1 = new int[11][11];
        chessArrays1[0][4] = 1;
        chessArrays1[8][5] = 1;
        chessArrays1[9][9] = 2;
        //将二维数组转化为稀疏数组 先得到二维数组的有效值 (遍历)
        int validValue = 0;
        for (int[] ints : chessArrays1) {
            for (int j = 0; j < 11; j++) {
                if (ints[j] != 0) {
                    validValue++;
                }
            }
        }
        //通过有效值创建一个稀疏数组
        int[][] sparseArray = new int[validValue + 1][3];
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = validValue;
        int mCount = 0;
        for (int i = 0; i < chessArrays1.length; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArrays1[i][j] != 0) {
                    mCount++;
                    sparseArray[mCount][0] = i;
                    sparseArray[mCount][1] = j;
                    sparseArray[mCount][2] = chessArrays1[i][j];
                }
            }
        }
        System.out.println("将二维数组转化为稀疏数组");
        for (int[] ints : sparseArray) {
            System.out.printf("%d\t %d\t %d\t \n", ints[0], ints[1], ints[2]);
        }
        //把数组保存到本地磁盘
        File file = new File("D:\\_WorkSpce\\NDK\\FFmpeg\\lib\\src\\main\\java\\com\\since\\lib", "save.data");
        if (file.exists()) {
            file.delete();
        }
        FileWriter writer = new FileWriter(file);
        for (int[] ints : sparseArray) {
            for (int anInt : ints) {
                writer.write(anInt + "\t");

            }
            writer.write("\r\n");
        }
        writer.close();
        System.out.println("保存的数组文件大小---->" + file.length());
        //从文件中读取数组
        int[][] sparseArray2 = new int[validValue + 1][3];
        FileReader reader = new FileReader(file);
        BufferedReader bf = new BufferedReader(reader);
        int row = 0;
        String line;
        while ((line = bf.readLine()) != null) {
            String[] temp = line.split("\t");
            for (int j = 0; j < temp.length; j++) {
                sparseArray2[row][j] = Integer.parseInt(temp[j]);
            }
            row++;
        }
        System.out.println("从文件中得到的稀疏数组");
        for (int[] ints : sparseArray2) {
            System.out.printf("%d\t %d\t %d\t \n", ints[0], ints[1], ints[2]);
        }
        //将稀疏数组转化为二维数组
        //通过有效值创建一个稀疏数组
        int[][] chessArray2 = new int[sparseArray2[0][0]][sparseArray2[0][1]];
        for (int i = 1; i < sparseArray2.length; i++) {
            chessArray2[sparseArray2[i][0]][sparseArray2[i][1]] = sparseArray2[i][2];
        }
        System.out.println("将稀疏数组转化为二维数组");
        for (int[] ints : chessArray2) {
            for (int data : ints) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }
}
